using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace LynxWebLog.Controllers
{
    [Authorize(Roles="BlogUser,BlogAdmin")]
    public class CategoryDataController : Controller
    {

        List<CategoryData> _lcd = new List<CategoryData>();
        HttpClient client = ClientUtility.BlogAPIClient();
        //
        // GET: /CategoryData/
        public async Task<ActionResult> Index()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            try {
                HttpResponseMessage resp = await client.GetAsync("api/CategoryData/?$filter=username eq '" + User.Identity.Name + "'");
                resp.EnsureSuccessStatusCode();

                IEnumerable<CategoryData> cd = await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>();
                _lcd = cd.ToList();
            }
            catch (SerializationException ex) {
                Debug.Print(ex.Message);
            }
            catch (HttpRequestException ex) {
                Debug.Print(ex.Message);
            }

            return View(_lcd);
        }

        //
        // GET: /CategoryData/Details/5
        public async Task<ActionResult> Details(int id)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            CategoryData cd = new CategoryData();
            try {
                HttpResponseMessage resp = await client.GetAsync("api/CategoryData/?$filter=username eq '" + User.Identity.Name + "' and categoryid eq " + id);
                resp.EnsureSuccessStatusCode();

                cd = (await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>()).FirstOrDefault();
            }
            catch (SerializationException ex) {
                Debug.Print(ex.Message);
            }
            catch (HttpRequestException ex) {
                Debug.Print(ex.Message);
            }
            return View(cd);
        }

        //
        // GET: /CategoryData/Create
        public ActionResult Create()
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            return View();
        }

        //
        // POST: /CategoryData/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        [MaliciousActivityChecksFilter]
        public async Task<ActionResult> Create(CategoryData cd)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            if (ModelState.IsValid) {
                try {
                    HttpResponseMessage resp = await client.PostAsXmlAsync("api/CategoryData", cd);
                    resp.EnsureSuccessStatusCode();

                    return RedirectToAction("Index");
                }
                catch (SystemException ex) {
                    Debug.Print(ex.Message);
                    //return View();
                }
            }
            return View();
        }

        //
        // GET: /CategoryData/Edit/5
        public async Task<ActionResult> Edit(int id)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            CategoryData cd = new CategoryData();
            try {
                HttpResponseMessage resp = await client.GetAsync("api/CategoryData/?$filter=username eq '" + User.Identity.Name + "' and categoryid eq " + id);
                resp.EnsureSuccessStatusCode();

                cd = (await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>()).FirstOrDefault();
            }
            catch (SystemException ex) {
                Debug.Print(ex.Message);
            }
            return View(cd);
        }

        //
        // POST: /CategoryData/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        [MaliciousActivityChecksFilter]
        public async Task<ActionResult> Edit(CategoryData cd)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            try {
                HttpResponseMessage resp = await client.PutAsXmlAsync("api/CategoryData/" + cd.categoryid, cd);
                resp.EnsureSuccessStatusCode();

                return RedirectToAction("Index");
            }
            catch (HttpRequestException ex) {
                Debug.Print(ex.Message);
                return View(cd );
            }
        }

        //
        // GET: /CategoryData/Delete/5
        public async Task<ActionResult> Delete(int id)
        {
            ViewBag.Version = " v: " + Assembly.GetExecutingAssembly().GetName().Version;
            CategoryData cd = new CategoryData();
            try {
                HttpResponseMessage resp = await client.GetAsync("api/CategoryData/?$filter=username eq '" + User.Identity.Name + "' and categoryid eq " + id);
                resp.EnsureSuccessStatusCode();

                cd = (await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>()).FirstOrDefault();
            }
            catch (SystemException ex) {
                Debug.Print(ex.Message);
            }
            return View(cd);
        }

        //
        // POST: /CategoryData/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        [MaliciousActivityChecksFilter]
        public async Task<ActionResult> DeleteConfirmed(int categoryid)
        {
            try {
                HttpResponseMessage resp = await client.DeleteAsync("api/CategoryData/" + categoryid);
                resp.EnsureSuccessStatusCode();

                return RedirectToAction("Index");
            }
            catch (SystemException ex) {
                Debug.Print(ex.Message);
                return RedirectToAction("Delete");
            }
        }
    }
}
